﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Data.Linq.Mapping;

using System.Collections.Generic;
using System.Text;
using System.Linq;
using BryhtCommon;

namespace MyRunDataBase
{
    [Table]
    public class SportModel : NotifyPropertyModelBase
    {
        public SportModel()
        {
            using (var db = new MyRunDataContent())
            {
                if (db.Sports.Count() > 0)
                {
                    Id = db.Sports.Max(p => p.Id) + 1;
                }
                else
                {
                    Id = 1;
                }

            }
        }

        private int _id = 0;
        [Column(IsPrimaryKey = true)]
        public int Id
        {
            get { return _id; }
            set
            {
                this.NotifyPropertyChanging(p => p.Id);
                _id = value;
                this.NotifyPropertyChanged(p => p.Id);
            }
        }

        private double _distance = 0;
        /// <summary>
        /// 距离
        /// </summary>
        [Column]
        public double Distance
        {
            get { return _distance; }
            set
            {
                this.NotifyPropertyChanging(p => p.Distance);
                _distance = value;
                this.NotifyPropertyChanged(p => p.Distance);
            }
        }

        private double _speed = 0;
        /// <summary>
        /// 速度(千米每小时)
        /// </summary>
        [Column]
        public double Speed
        {
            get { return _speed; }
            set
            {
                this.NotifyPropertyChanging(p => p.Speed);
                _speed = value;
                this.NotifyPropertyChanged(p => p.Speed);
            }
        }

        private string _duration = TimeSpan.Zero.ToString();
        /// <summary>
        /// 时间
        /// </summary>
        [Column]
        public string Duration
        {
            get { return _duration; }
            set
            {
                this.NotifyPropertyChanging(p => p.Duration);
                _duration = value;
                this.NotifyPropertyChanged(p => p.Duration);
            }
        }

        private DateTime _time = DateTime.Now;
        [Column]
        public DateTime Time
        {
            get { return _time; }
            set
            {
                this.NotifyPropertyChanging(p => p.Time);
                _time = value;
                this.NotifyPropertyChanged(p => p.Time);
            }
        }

        private double _chinaOffsetLongtitude = 0;
        [Column]
        public double ChinaOffsetLongtitude
        {
            get { return _chinaOffsetLongtitude; }
            set
            {
                this.NotifyPropertyChanging(p => p.ChinaOffsetLongtitude);
                _chinaOffsetLongtitude = value;
                this.NotifyPropertyChanged(p => p.ChinaOffsetLongtitude);
            }
        }

        private double _chinaOffsetLattitude = 0;
        [Column]
        public double ChinaOffsetLattitude
        {
            get { return _chinaOffsetLattitude; }
            set
            {
                this.NotifyPropertyChanging(p => p.ChinaOffsetLattitude);
                _chinaOffsetLattitude = value;
                this.NotifyPropertyChanged(p => p.ChinaOffsetLattitude);
            }
        }

        private bool _hasUpload = false;
        [Column]
        public bool HasUpload
        {
            get { return _hasUpload; }
            set
            {
                this.NotifyPropertyChanging(p => p.HasUpload);
                _hasUpload = value;
                this.NotifyPropertyChanged(p => p.HasUpload);
            }
        }

        //跑步热量（kcal）＝体重（kg）×距离（公里）×1.036 
        public double Calorie
        {
            get
            {
                var weight = Convert.ToDouble(Isolated.Get("Weight") ?? 70);//默认为70KG
                return weight * Distance * 1.036;
            }
        }


        public void GetAverageSpeed()
        {
            TimeSpan time = TimeSpan.Zero;
            TimeSpan.TryParse(Duration, out time);
            var totalHours = time.TotalHours;
            if (totalHours == 0)
            {
                Speed = 0;
            }
            else
            {
                Speed = Distance / TimeSpan.Parse(Duration).TotalHours;
            }
        }
    }
}
